package cn.jilian.entity;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ConstraintMode;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.PostLoad;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

import org.hibernate.annotations.Where;
import org.springframework.format.annotation.DateTimeFormat;

import cn.jilian.entity.BaseEntity;


@Entity
@Table(name="dt_order",uniqueConstraints = @UniqueConstraint(name="orderUniqueByFidAndName",columnNames = {"fid", "name" }) )
public class Order extends BaseEntity{
	
	
	
	/**
	 * 发布机构ID
	 * */
	private Long fid;
	
	/**
	 * 客户
	 * */
	@Transient
	private String supplier;
	/**
	 * 项目
	 * */
	private String name;
	
	/**
	 * 品牌
	 * */
	private String brand;
	
	
	
	@Column(name="start_date")
	@Temporal(TemporalType.DATE)
	private Date startDate;
	

	
	@Column(name="end_date")
	@Temporal(TemporalType.DATE)
	private Date endDate;
	
	@Column(name="start_addr")
	private String startAddr;
	
	@Column(name="end_addr")
	private String endAddr;
	
	@Column(name="contact")
	private String contact;
	
	@Column(name="mobile")
	private String mobile;
	
	@OneToMany(targetEntity=EleOrderItem.class,cascade = { CascadeType.ALL },fetch = FetchType.EAGER)
	@JoinColumn(name = "fid",foreignKey =@ForeignKey(name = "none",value = ConstraintMode.NO_CONSTRAINT))
	@Where(clause="meta="+EleOrderItem.ELE_META)
	@OrderBy("seq ASC")
	private List<EleOrderItem> eleOrderItems;
	
	@OneToMany(targetEntity=EscOrderItem.class,cascade = { CascadeType.ALL },fetch = FetchType.EAGER)
	@JoinColumn(name = "fid",foreignKey =@ForeignKey(name = "none",value = ConstraintMode.NO_CONSTRAINT))
	@Where(clause="meta="+EleOrderItem.ESC_META)
	@OrderBy("seq ASC")
	private List<EscOrderItem> escOrderItems;
	
	@Transient
	private Integer num=0;
	
	@PostLoad
	public void calNum() {
		Integer n=0;
		if(this.eleOrderItems!=null){
			n+=this.eleOrderItems.size();
		}
		if(this.escOrderItems!=null) {
			n+=this.escOrderItems.size();
		}
		this.num=n;
	}
	@PreUpdate
	@PrePersist
	public void setUpdateTims() {
		this.tims=new Date();
	}
	private Date tims;
	
	private Integer flag=1;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Long getFid() {
		return fid;
	}

	public void setFid(Long fid) {
		this.fid = fid;
	}
	

	@DateTimeFormat(iso=DateTimeFormat.ISO.DATE)
	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	
	@DateTimeFormat(iso=DateTimeFormat.ISO.DATE)
	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public String getStartAddr() {
		return startAddr;
	}

	public void setStartAddr(String startAddr) {
		this.startAddr = startAddr;
	}

	public String getEndAddr() {
		return endAddr;
	}

	public void setEndAddr(String endAddr) {
		this.endAddr = endAddr;
	}

	public String getContact() {
		return contact;
	}

	public void setContact(String contact) {
		this.contact = contact;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	@DateTimeFormat(iso=DateTimeFormat.ISO.DATE)
	public Date getTims() {
		return tims;
	}

	public void setTims(Date tims) {
		this.tims = tims;
	}
	@DateTimeFormat(iso=DateTimeFormat.ISO.DATE)
	public Integer getFlag() {
		return flag;
	}

	public void setFlag(Integer flag) {
		this.flag = flag;
	}

	public List<EleOrderItem> getEleOrderItems() {
		return eleOrderItems;
	}

	public void setEleOrderItems(List<EleOrderItem> eleOrderItems) {
		this.eleOrderItems = eleOrderItems;
	}

	public List<EscOrderItem> getEscOrderItems() {
		return escOrderItems;
	}

	public void setEscOrderItems(List<EscOrderItem> escOrderItems) {
		this.escOrderItems = escOrderItems;
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public String getSupplier() {
		return supplier;
	}

	public void setSupplier(String supplier) {
		this.supplier = supplier;
	}

	public Integer getNum() {
		return num;
	}

	public void setNum(Integer num) {
		this.num = num;
	}
	
	
}
